//
//  PopoverAnimator.swift
//  WeiBoDemo
//
//  Created by mac on 2017/11/21.
//  Copyright © 2017年 mac. All rights reserved.
//

import UIKit

class PopoverAnimator: NSObject {
    
    // MARK:- 属性
    var isPresented : Bool = false
    //开放frame大小属性
    var presentedFrame : CGRect = CGRect.zero
    //对外提供属性
    var callBackBlock : ((_ presented : Bool) -> ())?
    
    //MARK:-自定义构造函数
    
    init(call :@escaping (_ presented : Bool) -> ()) {
        super.init()
        self.callBackBlock = call
    }
}

//MARK: - 自定义转场代理的方法
extension PopoverAnimator : UIViewControllerTransitioningDelegate{
    //目的：改变弹出的View的尺寸
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        
        let presentation = WBPresentConreoller.init(presentedViewController: presented, presenting: presenting)
        presentation.presentedFrame = presentedFrame
        
        return presentation
    }
    //目的：自定义弹出的动画
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = true
        callBackBlock!(isPresented)
        return self
    }
    
    //目的：自定义消失的动画
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = false
        callBackBlock!(isPresented)
        return self
    }
}

//MARK:-弹出和消失动画
extension PopoverAnimator : UIViewControllerAnimatedTransitioning{
    //动画执行的时间
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.5
    }
    //获取“转场的上下文”：可以通过转场上下文获取弹出的View和消失的View
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        
        isPresented ? animationForPresentedView(transitionContext: transitionContext) : animationForDismissedView(transitionContext: transitionContext)
        
    }
    //自定义弹出动画
    private func animationForPresentedView(transitionContext: UIViewControllerContextTransitioning){
        //1.获取弹出的View
        // UITransitionContextFromViewKey,获取消失的View
        // UITransitionContextToViewKey   获取弹出的View
        let presentedView = transitionContext.view(forKey: UITransitionContextViewKey.to)
        //2.将弹出的View添加到containerView
        transitionContext.containerView.addSubview(presentedView!)
        //3.执行动画
        presentedView?.transform = CGAffineTransform.init(scaleX: 1.0, y: 0.0)
        //锚点修改位置
        presentedView?.layer.anchorPoint = CGPoint.init(x: 0.5, y: 0)
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            presentedView?.transform = CGAffineTransform.init(scaleX: 1.0, y: 1.0)
        }) { (_) in
            //必须告诉转场上下文你已经完成动画
            transitionContext.completeTransition(true)
        }
    }
    //自定义消失动画
    private func animationForDismissedView(transitionContext : UIViewControllerContextTransitioning){
        //1.获取消失的View
        let dismissView = transitionContext.view(forKey: UITransitionContextViewKey.from)
        //        执行动画
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            dismissView?.transform = CGAffineTransform.init(scaleX: 1.0, y: 0.000001)
        }) { (_) in
            //1.移除子视图
            dismissView?.removeFromSuperview()
            //必须告诉转场上下文你已经完成动画
            transitionContext.completeTransition(true)
        }
    }
}
